/**
 * Created by qiushaohua on 15-2-4.
 */
define(['app'], function (app) {

    'use strict';

    var deps = [];

    function service() {
        return {
            getConfig: function getConfig(type) {
                return EMOTIONS[type];
            },
            getEmotionImg: getEmotionImg,
            resolve: function resolve(html, option) {
                var type = option? option.emotionType : null;
                html = html.replace(EMOTION_REG, function () {
                    var name = arguments[1];

                    var config;
                    if (type) {
                        config = BIG_EMOTION_MAPS[type];
                    } else {
                        config = EMOTION_MAPS[name];
                    }

                    if (!config) {
                        // 没有这个表情, 忽略;
                        return arguments[0];
                    }
                    return '<img src="' + getEmotionImg(config, name) + '">';
                });

                return html;
            },
            reverse: function reverse(config, text) {
                if (typeof config === 'string') {
                    config = EMOTIONS[config];
                }
                if (!config.reverseRegex) {
                    var regStr = getEmotionImg(config, REVERSE_REG_STR);
                    regStr = '<img[^>]*?src="(http.*?/)?' + regStr + '"[^>]*?>';
                    config.reverseRegex = new RegExp(regStr, 'g');
                }
                return text.replace(config.reverseRegex, '[$2]');
            }
        };
    }

    var IMAGE_PATH = 'images/emoticons/';

    // \d+ 多个数字
    // [\u4e00-\u9fa5]+\d?  汉字, 后面带一个可选的数字
    // [a-z ]{4,} 4 位以上的字母或空格
    var EMOTION_REG = /\[(\d+|[\u4e00-\u9fa5]+\d?|[a-z ]{4,})\]/g;

    var REVERSE_REG_STR = '(\\d+|[\\u4e00-\\u9fa5]{2,}|[a-z ]{4,})';

    var EMOTIONS = {
        mengdoudou: {
            type: 4, // MNE
            emotions: [
                'cool', '不屑', '大哭', '大笑',
                '呆', '飞吻', '汗1', '汗2',
                '汗3', '哼', '哼歌', '惊呆了',
                '惊恐', '惊讶', '空姐笑', '哭',
                '哭3', '困', '怒', '亲亲',
                '色豆豆', '生气', '调皮', '调皮2',
                '吐舌头1', '吐舌头2', '吐舌头3', '吐舌头4',
                '晚安', '微笑', '无奈', '笑',
                '疑惑', '赞成'
            ],
            total: 34,
            rowsPerPage: 2,
            columnsPerRow: 4,
            prefix: 'mengdoudou/',
            suffix: '.gif',
            className: 'fc-emotion-icon-elephant',
            style: {
                'height': 93
            },
            showName: true,
            publish: true
        },
        goldElephant: {
            type: 3, // MNE
            emotions: [
                '新年快乐', '大吉大利', '红包拿来', '收礼物',
                '有钱任性', '财迷', '喜洋洋', '萌萌哒',
                '流口水', 'so easy', 'super man', '臭美',
                '羞羞', '泪奔', '呵呵', '怕怕'
            ],
            total: 16,
            rowsPerPage: 2,
            columnsPerRow: 4,
            prefix: 'goldElephant/',
            suffix: '.png',
            className: 'fc-emotion-icon-elephant',
            showName: true,
            publish: true

        },
        goldElephantSmall: {
            type: 3, // MNE
            emotions: makeNumericEmotionNames(16, 600),
            total: 16,
            rowsPerPage: 2,
            columnsPerRow: 4,
            prefix: 'goldElephantSmall/',
            suffix: '.png',
            className: 'fc-emotion-icon-elephant'
        },
        elephant: {
            type: 2, // ME
            emotions: [
                '生气', '微笑', '性感', '拥抱',
                '奸笑', '惊悚', '惊吓', '耍酷',
                '嘲笑', '大哭', '尴尬', '害羞',
                '热泪盈眶', '生病', '失落', '无聊',
                '卧躺', '顶球', '踢球', '自拍',
                '吃西瓜', '男红军', '女红军', '汉堡',
                '嫦娥', '圣诞', '万圣节', '美少女战士'
            ],
            total: 28,
            rowsPerPage: 2,
            columnsPerRow: 4,
            prefix: 'elephant/',
            suffix: '.png',
            className: 'fc-emotion-icon-elephant',
            showName: true,
            publish: true
        },
        elephantSmall: {
            type: 2, // ME
            emotions: makeNumericEmotionNames(28, 500),
            total: 28,
            rowsPerPage: 3,
            columnsPerRow: 8,
            prefix: '',
            suffix: '.png',
            className: 'fc-emotion-icon-elephantSmall'
        },
        normal: {
            type: 1, // 经典
            emotions: makeNumericEmotionNames(104),
            total: 104,
            rowsPerPage: 4,
            columnsPerRow: 10,
            prefix: '',
            suffix: '.gif',
            className: 'fc-emotion-icon-normal'
            //style: {
            //    width: 40,
            //    height: 40,
            //    padding: 8
            //}
        }
    };

    function makeNumericEmotionNames(total, base) {
        base = base || 0;
        var arr = [];
        for (var i = 0; i < total; i++) {
            arr.push('' + (base + i));
        }
        return arr;
    }

    function updateEmotionConfig() {
        for (var name in EMOTIONS) {
            if (EMOTIONS.hasOwnProperty(name)) {
                EMOTIONS[name].name = name;
            }
        }
    }
    updateEmotionConfig();

    function reverseEmotionConfig() {
        var map = {};
        for (var name in EMOTIONS) {
            if (EMOTIONS.hasOwnProperty(name) && !EMOTIONS[name].publish) {
                var emotions = EMOTIONS[name].emotions;
                for (var i = 0, len = emotions.length; i < len; i++) {
                    var emotion = emotions[i];
                    map[emotion] = EMOTIONS[name];
                }
            }
        }
        return map;
    }
    var EMOTION_MAPS = reverseEmotionConfig();

    /**
     * 大表情, 配置了publish: true 的那些表情, 点了之后直接上屏的那种.
     * 这个有可能出现重名, 所以根据类型来进行处理先, 也加上才行了.
     */
    function reverseBigEmotionConfig() {
        var map = {};
        for (var name in EMOTIONS) {
            if (EMOTIONS.hasOwnProperty(name) && EMOTIONS[name].publish) {
                var type = EMOTIONS[name].type;
                map[type] = EMOTIONS[name];
            }
        }
        return map;
    }
    var BIG_EMOTION_MAPS = reverseBigEmotionConfig();

    function getEmotionImg(config, name) {
        if (typeof config === 'string') {
            config = EMOTIONS[config];
        }
        if (typeof name === 'number') {
            name = config.emotions[name];
        }
        return IMAGE_PATH + config.prefix + name + config.suffix;
    }

    service.$inject = deps;
    app.lazy.service('EmotionService', service);
});
